package com.kolaxy.social.govern.report.controller;

import com.google.common.collect.Lists;
import com.kolaxy.social.govern.report.domain.ReportTask;
import com.kolaxy.social.govern.report.service.ReportTaskService;
import com.kolaxy.social.govern.report.service.ReportTaskVoteService;
import com.kolaxy.social.govern.reviewer.ReviewerService;
import com.kolaxy.social.govern.reviewer.RewardService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Description 举报
 * @Author zrj
 * @Date 2020/9/1 12:53
 * @Version 1.0
 */
@RestController
public class ReportController {

    @Reference(version = "1.0.0", interfaceClass = ReviewerService.class, cluster = "failfast")
    private ReviewerService reviewerService;

    @Reference(version = "1.0.0", interfaceClass = RewardService.class, cluster = "failfast")
    private RewardService rewardService;

    @Autowired
    private ReportTaskService reportTaskService;


    @Autowired
    private ReportTaskVoteService reportTaskVoteService;

    /**
     * 举报
     * @return
     */
    @GetMapping("/report")
    public String report(ReportTask reportTask) {



        //本地数据库增加一个举报任务
        reportTaskService.add(reportTask);

        //选择一批评审员
        List<Long> reivewerIds = reviewerService.selectReivewers(reportTask.getId());

        //在本地数据库初始化这批评审员低举报任务的投票
        reportTaskVoteService.initVotes(reivewerIds, reportTask.getTargetId());

        //push

        return "success";
    }

    @GetMapping("/report/query/{id}")
    public ReportTask queryReportTask(@PathVariable("id") Long reportTaskId) {
        return reportTaskService.queryById(reportTaskId);
    }

    @GetMapping("/report/vote")
    public String vote(Long reviewerId, Long reportTaskId, Integer voteReport) {
        reportTaskVoteService.vote(reviewerId, reportTaskId, voteReport);
        reviewerService.finishVote(reviewerId, reportTaskId);
        reportTaskVoteService.calculateVotes(reportTaskId);
        rewardService.giveReward(Lists.newArrayList(reviewerId));
        return "success";
    }

}
